﻿///#source 1 1 /App/app.main.js
window.app = angular.module('app', ['ngResource']);

app.value('toastr', window.toastr);

app.config(['$routeProvider', '$locationProvider', '$httpProvider',
    function ($routeProvider, $locationProvider, $httpProvider) {

        $routeProvider.when('/', {
            redirectTo: '/home'
        });

        $routeProvider.when('/home', {
            templateUrl: 'Views/home.html',
            controller: 'HomeCtrl',
        });

        $routeProvider.when('/people', {
            templateUrl: 'Views/people.html',
            controller: 'PeopleCtrl',
            resolve: {
                people: ['Person', function(Person) {
                    return Person.all();
                }]
            }
        });

        $routeProvider.when('/about', {
            templateUrl: 'Views/about.html',
            controller: 'AboutCtrl',
        });

        $routeProvider.otherwise({
            redirectTo: '/'
        });

        $locationProvider.html5Mode(false).hashPrefix('!');
        $httpProvider.responseInterceptors.push('errorHttpInterceptor');
    }]);

app.run(['$rootScope', function($rootScope) {
    $rootScope.$on('$routeChangeSuccess', function(obj, arg) {
        $rootScope.templateUrl = arg.loadedTemplateUrl;
    });
}]);
///#source 1 1 /App/Services/errors.js
app.factory('errorHttpInterceptor', ['$q', '$location', 'toastr', '$rootScope',
    function($q, $location, toastr, $rootScope) {
        return function(promise) {
            return promise.then(function(response) {
                return response;
            }, function (response) {
                if (response.status === 0) {
                    toastr.error('Server connection lost');
                } else if (response.status === 401) {
                    $rootScope.$broadcast('event:loginRequired');
                } else if (response.status >= 400 && response.status < 500) {
                    toastr.error('Server was unable to find' +
                        ' what you were looking for... Sorry!!');
                } else {
                    toastr.error('Error ' + response.status + ': ' + response.data);
                }
                return $q.reject(response);
            });
        };
    }]);
///#source 1 1 /App/Services/model.js
app.factory('Person',
    ['$resource',
        function($resource) {
            return $resource('api/people/:id', { id: '@Id' },
                {
                    all: { method: 'GET', isArray: true },
                    add: { method: 'POST' },
                    get: { method: 'GET' },
                    save: { method: 'PUT' },
                    remove: { method: 'DELETE' },
                }
            );
        }]);
///#source 1 1 /App/Controllers/home.js
app.controller('HomeCtrl', ['$scope',
    function($scope) {

    }]);
///#source 1 1 /App/Controllers/people.js
app.controller('PeopleCtrl', ['$scope', 'Person', 'toastr', 'people',
    function ($scope, Person, toastr, people) {

        $scope.people = people;
        $scope.edit = new Person();
        $scope.editMode = false;

        $scope.savePerson = function() {
            if ($scope.editMode) {
                $scope.edit.$save(function() {
                    toastr.success('Save ' + $scope.edit.Name + ' success');
                    $scope.edit = new Person();
                    $scope.editMode = false;
                });
            } else {
                $scope.edit.$add(function() {
                    $scope.people.push($scope.edit);
                    toastr.success('Add ' + $scope.edit.Name + ' success');
                    $scope.edit = new Person();
                });
            }
            
        };

        $scope.removePerson = function (person) {
            person.$remove(function() {
                for (var i = 0; i < $scope.people.length; i++) {
                    if ($scope.people[i] === person) {
                        $scope.people.splice(i, 1);
                        toastr.success('Remove ' + person.Name + ' success');
                        return;
                    }
                }
            });
        };

        $scope.editPerson = function(person) {
            if (person) {
                $scope.editMode = true;
                $scope.edit = person;
            }
        };

        $scope.cancelEdit = function() {
            $scope.edit = new Person();
            $scope.editMode = false;
        };

    }]);
///#source 1 1 /App/Controllers/about.js
app.controller('AboutCtrl', ['$scope',
    function ($scope) {
        $scope.title = 'About';
    }]);
